package dream.web;

import cn.hutool.json.JSONObject;
import dream.pojo.AppVar;
import dream.pojo.User;
import dream.util.SessionUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 得到当前登录用户的信息
 */
@WebServlet("/loginUserInfo")
public class LoginUserInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JSONObject object = new JSONObject();

        User user = SessionUtils.getLoginUser(request);
        if (user != null) {
            //当前已登录
            user.setPassword("");//去掉返回信息中的敏感信息

            //将用户信息返回给前端
            response.setContentType(AppVar.RESPONSE_CONTENTTYPE_JSON);
            object.put("username", user.getUsername());
            object.put("id", user.getId());
            response.getWriter().println(object);

        } else {
            return;
        }
    }
}
